<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    :width="!props.isReadonly ? '900px' : '900px'"
    draggable
    @close="closeDialog1"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="formData.q8dExceptionAssignDTO"
      :rules="rules"
      label-width="100"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="问题编号" prop="qualityExceptionCode">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.qualityExceptionCode"
              placeholder="请输入问题编号"
              clearable
            />
            <span v-else>{{
              formData.q8dExceptionAssignDTO.qualityExceptionCode
            }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="客诉编号" prop="proposeDepartmentName">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.exceptionAssignCode"
              placeholder="请输入客诉编号"
              clearable
            />
            <span v-else>{{
              formData.q8dExceptionAssignDTO.exceptionAssignCode
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开始日期" prop="createTime" style="width: 100%">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.startDate"
              placeholder="请输入开始日期"
              clearable
            />
            <span v-else>{{ formData.q8dExceptionAssignDTO.startDate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="内容分类" prop="contentTypeId">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.contentTypeId"
              placeholder="请选择问题处理方式"
              clearable
              style="width: 100%"
              @change="handleChangeContentCategory"
            >
              <el-option
                v-for="item in contentCategoryList"
                :key="item.id"
                :label="item.exceptionContentType"
                :value="String(item.id)"
              />
            </el-select>
            <span v-else>{{
              formData.q8dExceptionAssignDTO.contentTypeId
            }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="特性分类" prop="propertySortId">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.propertySortId"
              placeholder="请输入特性分类"
              clearable
              style="width: 100%"
              @change="handleChangecharacteristicList"
            >
              <el-option
                v-for="item in characteristicList"
                :key="item.id"
                :label="item.exceptionPeculiarityTypeCode"
                :value="String(item.id)"
              />
            </el-select>
            <span v-else>{{
              formData.q8dExceptionAssignDTO.propertySortId
            }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item
            label="供应商名称"
            prop="supplyId"
            v-if="formData.q8dExceptionAssignDTO.supplyId != ''"
          >
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.supplyName"
              placeholder="请输入供应商名称"
              clearable
            />
            <span v-else>{{ formData.q8dExceptionAssignDTO.supplyName }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="零件图号" prop="figureNumber">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.figureNumber"
              placeholder="请输入零件图号"
              clearable
              style="width: 100%"
              @change="changeFigureNumber"
            >
              <el-option
                v-for="item in materialProcessList"
                :key="item.id"
                :label="item.materialProcessFigure"
                :value="String(item.id)"
              />
            </el-select>
            <span v-else>{{
              formData.q8dExceptionAssignDTO.figureNumber
            }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="零件名称" prop="materialCode">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.q8dExceptionAssignDTO.materialName"
              placeholder="请输入零件名称"
              clearable
            />
            <span v-else>{{
              formData.q8dExceptionAssignDTO.materialName
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="涉及数量" prop="exceptionQuantity">
            <el-input-number
              v-model="formData.q8dExceptionAssignDTO.exceptionQuantity"
              :min="1"
              :precision="0"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <div style="display: flex; flex-direction: column; margin-top: 20px">
            问题说明：
            <el-input
              :autosize="{ minRows: 6, maxRows: 6 }"
              v-model="formData.q8dExceptionAssignDTO.exceptionDescription"
              maxlength="255"
              style="width: 850px"
              placeholder="请输入"
              show-word-limit
              type="textarea"
            />
          </div>
        </el-col>
        <div
          style="
            margin-top: 20px;
            width: 100%;
            max-height: 200px;
            overflow-y: auto;
          "
          id="table"
        >
          <CommonUploadFile
            :isRead="props.isReadonly"
            :files="formData.q8dExceptionAssignDTO.assetPhotos"
            @update-files="updateFormFiles"
          />
        </div>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="backStep"
          >上一步</el-button
        >
        <el-button
          v-if="!props.isReadonly"
          :loading="loading"
          @click="handleSubmit"
          >提交</el-button
        >
        <el-button
          v-if="!props.isReadonly"
          :loading="loading"
          type="primary"
          @click="handleSave"
          >保存</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import dayjs from "dayjs";
// import html2pdf from "./PDF.js";
const dictionaryStore = useDictStoreHook();

function PDFprit() {
  const table = document.getElementById("table");
  // html2pdf(table, {
  //   background: "#fff",
  //   minimumUnit: "tr",
  //   orientation: "l",
  // });
  // html2pdf("table", "对账单", dayjs(new Date()).format("YYYY-mm-dd"));
}
defineOptions({
  name: "Q8FEEDBACKREPORTDIT",
  inheritAttrs: false,
});

const emit = defineEmits([
  "reset-table",
  "change-visible2",
  "backList2",
  "goback2",
]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;

    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productionTypeId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productionTypeName: [
    // { required: true, message: "请选择产品类目名称", trigger: "change" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [
    // { required: true, message: "请选择图号", trigger: "change" },
  ],
  qualityQuantity: [
    // { required: true, message: "请输入数量", trigger: "blur" },
  ],
  failureQuantity: [
    // { required: true, message: "请输入失效对应数量", trigger: "blur" },
  ],
  failureMode: [
    // { required: true, message: "请选择失效模式", trigger: "change" },
  ],
  occurrenceTime: [
    // { required: true, message: "请选择发生时间", trigger: "change" },
  ],
  occurrenceLocation: [
    // { required: true, message: "请选择发生地点", trigger: "change" },
  ],
  informationSourceId: [
    // { required: true, message: "请选择信息来源", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  informationSourceContent: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  exceptionState: [],
  isInternal: [],
  supplyId: [
    // { required: true, message: "请选择生产单位", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  problemTypeCode: [
    // { required: true, message: "请选择问题分类", trigger: "change" },
  ],
  lineProcessName: [
    // { required: true, message: "请输入生产线/过程", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productProcess: [
    // { required: true, message: "请选择产品/过程状态", trigger: "change" },
  ],
  problemFrequency: [
    // { required: true, message: "请输入问题发生频度", trigger: "blur" },
  ],
  //   lineProcessName: [
  //     // { required: true, message: "请选择责任部门", trigger: "change" },
  //   ],

  exceptionDescription: [
    // { required: true, message: "请输入", trigger: "blur" },
  ],

  propertySortId: [
    // { required: true, message: "请选择特性分类", trigger: "change" },
  ],
  contentTypeId: [
    // { required: true, message: "请选择内容分类", trigger: "change" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityProblemCode: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  informationSourceId: "",
  lineProcessName: "",
  productProjectName: "",
  sortCodeId: "",
  sortCode: "",
  productProcess: "",
  failureMode: "",
  consequenceInfluence: "",
  problemFrequency: "",
  proposeDepartmentId: "",
  proposeDepartmentCode: "",
  proposeDepartmentName: "",
  proposeManagerId: "",
  proposeManagerName: "",
  departmentId: "",
  departmentCode: "",
  departmentName: "",
  departmentManagerId: "",
  departmentManagerName: "",
  isInternal: 1,
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  problemTime: "",
  problemUserId: "",
  problemUserName: "",
  problemTypeCode: "",
  exceptionTypeSubId: "",
  exceptionTypeSubCode: "",
  exceptionTypeSubName: "",
  exceptionLevel: "",
  problemApproachId: "",
  problemApproachCode: "",
  problemApproachType: "",
  problemApproachName: "",
  releaseState: "",
  auditUserId: "",
  auditUserName: "",
  provisionalMeasuresDeadline: "",
  rectificationDeadline: "",
  rectificationRequirement: "",
  agreeWith: "",
  approvalOpinion: "",
  approvalTime: "",
  targetTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  // 涉及数量
  qualityQuantity: 1,
  // 失效模式
  failureModeContent: "",
  // 发生地点
  occurrenceLocation: "",
  // 状态
  productProcessContent: "",
  sortName: "",
  // 图号编号
  materialProcessCode: "",
  materialProcessName: "",
  materialProcessId: "",
  materialProcessFigure: "",
  // 问题处理方式
  exceptionSolvingType: "",
  // 问题级别
  exceptionLevelCode: "",
  exceptionLevelName: "",
  exceptionLevelContent: "", // 8D问题反馈报告
  exceptionLevelId: "",
  assetPhotos: [],
  q8dExceptionAssignDTO: {
    id: "",
    qualityExceptionId: "",
    qualityExceptionCode: "",
    exceptionAssignCode: "",
    customerComplaintCode: "",
    startDate: "",
    contentTypeId: "",
    contentType: "",
    propertySortId: "",
    propertySort: "",
    supplyId: "",
    supplyCode: "",
    supplyName: "",
    materialId: "",
    materialCode: "",
    materialName: "",
    figureNumber: "",
    exceptionQuantity: 1,
    exceptionDescription: "",
    exceptionAssignState: "",
    assetPhotos: [],
    version: "",
    remarks: "",
    createTime: "",
    createdUserId: "",
    createdUserName: "",
    lastUpdateTime: "",
    lastUpdatedUserId: "",
    lastUpdatedUserName: "",
    enabled: "",
    deleted: "",
  },
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      console.log(111111111);

      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        // 图号编号
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevelName,
        exceptionLevelContent,
        q8dExceptionAssignDTO,
        exceptionLevelId,
        assetPhotos,
      } = newVal;
      Object.assign(formData, {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevelName,
        exceptionLevelContent,
        q8dExceptionAssignDTO: q8dExceptionAssignDTO ?? {},
        exceptionLevelId,
        assetPhotos,
      });
      formData.q8dExceptionAssignDTO.assetPhotos ??= [];
      if (formData.problemTypeCode == "1") {
        problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
      } else {
        problemApproachTypeList.value = [
          { value: "1", label: "简单问题处理" },
          { value: "2", label: "问题8D处理" },
        ];
      }
      // getPersonList(formData.departmentId);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible2", false);
  resetForm();
}
/** 关闭表单弹窗 */
function closeDialog1() {
  // 如果使上一步 就把from传过去 不然就使关闭弹窗和取消按钮不弹窗
  if (backBtn.value) {
    visibleDialog.value = false;
    emit("backList2", false, formData);
    backBtn.value = false;
  } else {
    visibleDialog.value = false;
    emit("change-visible2", false);
    resetForm();
  }
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityProblemCode = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.informationSourceId = "";
  formData.lineProcessName = "";
  formData.productProjectName = "";
  formData.sortCodeId = "";
  formData.sortCode = "";
  formData.productProcess = "";
  formData.failureMode = "";
  formData.consequenceInfluence = "";
  formData.problemFrequency = "";
  formData.proposeDepartmentId = "";
  formData.proposeDepartmentCode = "";
  formData.proposeDepartmentName = "";
  formData.proposeManagerId = "";
  formData.proposeManagerName = "";
  formData.departmentId = "";
  formData.departmentCode = "";
  formData.departmentName = "";
  formData.departmentManagerId = "";
  formData.departmentManagerName = "";
  formData.isInternal = 1;
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.problemTime = "";
  formData.problemUserId = "";
  formData.problemUserName = "";
  formData.problemTypeCode = "";
  formData.exceptionTypeSubId = "";
  formData.exceptionTypeSubCode = "";
  formData.exceptionTypeSubName = "";
  formData.exceptionLevel = "";
  formData.problemApproachId = "";
  formData.problemApproachCode = "";
  formData.problemApproachType = "";
  formData.problemApproachName = "";
  formData.releaseState = "";
  formData.auditUserId = "";
  formData.auditUserName = "";
  formData.provisionalMeasuresDeadline = "";
  formData.rectificationDeadline = "";
  formData.rectificationRequirement = "";
  formData.agreeWith = "";
  formData.approvalOpinion = "";
  formData.approvalTime = "";
  formData.targetTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.qualityQuantity = 1;
  formData.occurrenceLocation = "";
  formData.productProcessContent = "";
  formData.sortName = "";
  formData.materialProcessCode = "";
  formData.materialProcessName = "";
  formData.materialProcessId = "";
  formData.materialProcessFigure = "";
  formData.exceptionSolvingType = "";
  formData.exceptionLevelCode = "";
  formData.exceptionLevelName = "";
  formData.exceptionLevelContent = "";
  formData.exceptionLevelId = "";
  formData.assetPhotos = [];
  formData.q8dExceptionAssignDTO = JSON.parse(
    JSON.stringify(q8dExceptionAssignDTO)
  );
}
const q8dExceptionAssignDTO = {
  id: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  exceptionAssignCode: "",
  customerComplaintCode: "",
  startDate: "",
  contentTypeId: "",
  contentType: "",
  propertySortId: "",
  propertySort: "",
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  figureNumber: "",
  exceptionQuantity: 1,
  exceptionDescription: "",
  exceptionAssignState: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  assetPhotos: [],
};
const loading = ref(false);

/** 质量异常信息保存提交 */
function handleSubmit() {
  console.log("提交", formData);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      formData.assetPhotos = formData.q8dExceptionAssignDTO.assetPhotos;
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/qms/q8dqualityproblem/publish",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("goback2", false);
      emit("reset-table", formData.id);
    }
  });
}
/** 质量异常信息保存 */
function handleSave() {
  console.log(formData);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "保存" : "保存";
      await request({
        url: "/qms/q8dqualityproblem/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("goback2", false);
      emit("reset-table", formData.id);
    }
  });
}
// // 产品过程状态列表
// const materialProcessStateList = ref([]);
// const getMaterialProcessStateList = async () => {
//   let res = await request({
//     url: "/qms/qmsmaterialprocessstate/getList",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   console.log("产品状态", res.data);

//   materialProcessStateList.value = res.data;
// };

// // 改变产品状态
// const handleChangeMaterialProcessState = (e) => {
//   if (e) {
//     console.log(e);
//   }
// };

// // 供应商列表
// const supplyList = ref([]);
// async function getSupplyList() {
//   let res = await request({
//     url: "mdm/mdmpartner/getListByFull",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   supplyList.value = res.data;
// }

// const changSupplyList = (e) => {
//   if (e) {
//     supplyList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.supplyName = item.partnerName;
//         formData.supplyCode = item.partnerCode;
//       }
//     });
//   } else {
//     formData.supplyName = "";
//     formData.supplyCode = "";
//   }
// };

// const changIsInternal = (e) => {
//   console.log(e);
//   formData.supplyId = "";
//   formData.supplyName = "";
//   formData.supplyCode = "";
// };

// 责任部门

const organizationList = ref([]);
const getOrganizationList = async () => {
  let res = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 4 },
  }).finally(() => {
    loading.value = false;
  });
  organizationList.value = res.data;
  console.log("organizationList", organizationList.value);
};

const handleChangeDepartment = (e) => {
  if (e) {
    // getPersonList(e.id);
    formData.departmentName = e.organizationName;
    formData.departmentCode = e.organizationCode;
  } else {
    formData.departmentName = "";
    formData.departmentCode = "";
    formData.departmentManagerName = "";
  }
};

// 责任人
const personList = ref([]);
const getPersonList = async (id) => {
  const res = await request({
    url: "/system/sysUser/getList ",
    method: "post",
    data: {
      departmentId: id,
    },
  });
  if (res.data) {
    personList.value = res.data;
    console.log("personList", personList.value);
  }
};
// 改变责任人
const handleChangePerson = (e) => {
  if (e) {
    personList.value.forEach((item) => {
      if (item.id == e) {
        formData.departmentManagerName = item.firstName;
      }
    });
  } else {
    formData.departmentManagerName = "";
  }
};

// 失效模式树列表
const failureModeList = ref([]);
async function getFailureMode() {
  let res = await request({
    url: "/qms/qmsfailuremode/getTree",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  failureModeList.value = res.data;
  console.log("failureMode", failureModeList.value);
}

const handleNodeClick = (e) => {
  if (e) {
    formData.failureModeContent = e.failureModeContent;
  } else {
    formData.failureModeContent = "";
  }
};

// 图号编号列表
const materialProcessList = ref([]);
async function getMaterialProcess() {
  let res = await request({
    url: "/qms/qmsmaterialprocess/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  materialProcessList.value = res.data;
  console.log("materialProcessList", materialProcessList.value);
}
const changeFigureNumber = (e) => {
  if (e) {
    materialProcessList.value.forEach((item) => {
      if (item.id == e) {
        formData.q8dExceptionAssignDTO.materialCode = item.materialProcessCode;
        formData.q8dExceptionAssignDTO.materialName = item.materialProcessName;
        formData.q8dExceptionAssignDTO.figureNumber =
          item.materialProcessFigure;
      }
    });
  } else {
    formData.q8dExceptionAssignDTO.materialCode = "";
    formData.q8dExceptionAssignDTO.materialName = "";
    formData.q8dExceptionAssignDTO.figureNumber = "";
  }
};

// 问题级别
const problemLevelList = ref([]);
const getProblemLevelList = async () => {
  let res = await request({
    url: "/qms/qmsexceptionlevel/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  problemLevelList.value = res.data;
  console.log("problemLevelList", problemLevelList.value);
};

// // 选择问题级别
// const handleChangeProblemLevel=(e)=>{
//   problemLevelList.value.forEach((item)=>{
//     if(item.id==e){
//       formData.exceptionLevel=item.id
//     }
//   })
// }
// 改变问题分类获取问题处理方式类型
const problemApproachTypeList = ref([]);
const problemTypeChange = () => {
  if (formData.problemTypeCode == "1") {
    problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
  } else {
    problemApproachTypeList.value = [
      { value: "1", label: "简单问题处理" },
      { value: "2", label: "问题8D处理" },
    ];
  }
};

// 上一步
let backBtn = ref(false);
const backStep = () => {
  // 预防纠正措施弹窗
  backBtn.value = true;
  visibleDialog.value = false;
};

// 内容分类
const contentCategoryList = ref([]);
const getContentCategoryList = async () => {
  let res = await request({
    url: "/qms/qmsexceptioncontenttype/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  contentCategoryList.value = res.data;
  console.log("contentCategoryList", contentCategoryList.value);
};
// 选择内容分类
const handleChangeContentCategory = (e) => {
  if (e) {
    contentCategoryList.value.forEach((item) => {
      if (item.id == e) {
      }
    });
  }
};
// 问题特性分类
const characteristicList = ref([]);
const getCharacteristicList = async () => {
  let res = await request({
    url: "/qms/qmsexceptionpeculiaritytype/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  characteristicList.value = res.data;
  console.log("characteristicList", characteristicList.value);
};
// 选择问题特性分类
const handleChangecharacteristicList = (e) => {
  if (e) {
    characteristicList.value.forEach((item) => {
      if (item.id == e) {
      }
    });
  }
};

// 图片处理
function updateFormFiles(val: []) {
  formData.q8dExceptionAssignDTO.assetPhotos = val;
}

onMounted(() => {
  getContentCategoryList();
  getCharacteristicList();
  // getFailureMode();
  // getMaterialProcessStateList();
  getOrganizationList();
  // getSupplyList();
  getMaterialProcess();
  getProblemLevelList();
});
// // 组件销毁时调用
// onBeforeUnmount(() => {
//   visibleDialog.value = false;
//   emit("change-visible2", false);
// });
</script>
